﻿Public Class Children

    Private Sub Children_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(DataGridView1)
        loadkid()
        'btnadd.Visible = False
        'btndel.Visible = False
        'btnupd.Visible = False
        loadtotal()
    End Sub

    Private Sub loadkid()
        Dim ti = Now.Year
        cmdp.Parameters("@search") = "%" & txtsearch.Text & "%"
        DataGridView1.DataSource = cmdp.ExecuteDataTable("SELECT r.RelateId,e.EmpId,s.SexId,r.RelateName,r.RelateDOB," & ti & "-YeAR(r.RelateDOB) as [Age],s.SexName,e.EmpName FROM [Relative] r,[Sex] s,[Employee] e WHERE s.SexId=r.SexId AND e.EmpId=r.EmpId AND r.Active=true AND r.TypeId=2 AND (r.RelateName+e.EmpName+s.Sexname) like (@search)")
    End Sub
    Private Sub loadtotal()
        lbltotal.Text = cmdp.ExecuteScalar("SELECT COUNT(*) FROM [Relative] WHERE Active=true AND TypeId=2")
    End Sub
    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        loadkid()
    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        Dim frm As New NewKid
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loadkid()
        End If
    End Sub

    Private Sub btnupd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupd.Click
        If DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim frm As New NewKid
        'frm.ShowDialog()
        Dim row = DataGridView1.SelectedRows(0)
        frm.eid = row.Cells("eid").Value
        frm.txtname.Text = row.Cells("ChildName").Value
        frm.dtpdob.Value = row.Cells("dob").Value
        frm.sid = row.Cells("SexId").Value
        frm.cid = row.Cells("reId").Value
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loadkid()
        End If
        'MsgBox(frm.cid & frm.eid)
    End Sub

    Private Sub btndel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndel.Click
        If DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = DataGridView1.SelectedRows(0)

        cmdp.ExecuteNonQuery("UPDATE [Relative] SET Active=false WHERE RelateId=" & row.Cells("reId").Value & "")

        Dim r As DataGridViewRow
        r = DataGridView1.SelectedRows(0)
        Me.DataGridView1.Rows.Remove(r)
    End Sub
End Class